package org.example.json.enumeration;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

public class ColorEnumDeserializer extends JsonDeserializer<Color> {
    @Override
    public Color deserialize(JsonParser p, DeserializationContext context) throws IOException {
        switch (p.getCurrentToken()) {
            case VALUE_STRING:
                if (context.isEnabled(DeserializationFeature.READ_ENUMS_USING_TO_STRING)) {
                    return Color.valueOf(p.getValueAsString());
                } else {
                    return Color.valueOf(p.getValueAsInt());
                }
            case VALUE_NUMBER_INT:
                return Color.valueOf(p.getIntValue());
            default:
                return (Color) context.handleUnexpectedToken(Color.class, p);
        }
    }
}